#coding:utf-8
'''
create on 2017-04-20
@author:sandy
'''

from pycommon import Pack as packet
from pycommon import Const,Utils
import GateManager
import GameManager

################################################## To Client ################################################
def loginSucess(code):
    return packet.pack("HH",Const.CMD_CT2C_USER_LOGIN,code)

def sendBaseInfoToClient(uid,nickname,photo,sex):
    return packet.pack("HIsSB",Const.CMD_CT2C_BASEINFO,uid,Utils.u2pstr(nickname),Utils.u2pstr(photo),sex)
    pass

def sendPropToClient(propmap):
    lc = len(propmap.keys())
    ret = packet.pack("HH",Const.CMD_CT2C_PROP, lc)
    for pi in propmap:
        s = packet.pack("IH",pi,propmap.get(pi))
        ret += s
    return ret
    pass

def sendPropertyToClient(propertyMap):
    lc = len(propertyMap.keys())
    ret = packet.pack("HH",Const.CMD_CT2C_PROPERTY, lc)
    for pi in propertyMap:
        s = packet.pack("IH",pi,propertyMap.get(pi))
        ret += s
    return ret
    pass

def sendCreateTableToClient(code,tableIdStr):
    return packet.pack("HHs",Const.CMD_CT2C_CREATE_TABLE,code,tableIdStr)
    pass

def sendEnterTableToClient(code,ip,port,tableId,hallId):
    return packet.pack("HHsHHH",Const.CMD_CT2C_ENTER_TABLE,code,Utils.u2pstr(ip),port,tableId,hallId)
    pass


################################################## To Game   ################################################
def gameLoginCenterSucess():
    return packet.pack("H",Const.CMD_CT2GAME_LOGINSUC)

def checkUser(code,uid, cid, session, nickname=u'',photo=u'',sex=0):
    ret = packet.pack("HHIHs",Const.CMD_CT2GAME_CHECK_USER,code,uid,cid,session)
    if code == Const.ERR_OK:
        ret += packet.pack("ssB",Utils.u2pstr(nickname),Utils.u2pstr(photo),sex)
    return ret

def kickUserFromGame(uid,pid,code):
    return packet.pack("HIHH",Const.CMD_CT2GAME_KICK_USER,uid,pid,code)

def sendInfoEndToGame(uid):
    return packet.pack("HI",Const.CMD_CT2GAME_INFO_END,uid)
    pass

def sendPropToGame(uid,propmap):
    lc = len(propmap.keys())
    ret = packet.pack("HIH",Const.CMD_CT2GAME_PROP, uid ,lc)
    for pi in propmap:
        s = packet.pack("IH",pi,propmap.get(pi))
        ret += s
    return ret
    pass

def sendPropertyToGame(uid,propertyMap):
    lc = len(propertyMap.keys())
    ret = packet.pack("HIH",Const.CMD_CT2GAME_PROPERTY, uid,lc)
    for pi in propertyMap:
        s = packet.pack("IH",pi,propertyMap.get(pi))
        ret += s
    return ret
    pass

def sendCreateTableToGame(ctxid,uid,maxRound,hallId,playModel,tableIdStr):
    return packet.pack("HHIHHHs",Const.CMD_CT2GAME_CREATE_TABLE,ctxid,uid,maxRound,hallId,playModel,tableIdStr)
    pass

################################################## To Gate  #################################################
def gateLoginCenterSucess():
    gameInfo = GameManager.instance.getGameInfoJson()
    return packet.pack("HS",Const.CMD_CT2GATE_LOGINSUC,gameInfo)

def gameUpdate():
    gameInfo = GameManager.instance.getGameInfoJson()
    return packet.pack("HS",Const.CMD_CT2GATE_GAME_UPDATE,gameInfo)

def kickUserFromGate(uid,pid,code):
    return packet.pack("HIHH",Const.CMD_CT2GATE_KICK_USER,uid,pid,code)



################################################# To Login ###################################################
def loginLoginCenterSucess():
    gateInfo = GateManager.instance.getGateInfoJson()
    return packet.pack("HS",Const.CMD_CT2LG_LOGINSUC,gateInfo)


def gateUpdate():
    gateInfo = GateManager.instance.getGateInfoJson()
    return packet.pack("HS",Const.CMD_CT2LG_GATE_UPDATE,gateInfo)


################################################# To Bill ###################################################
def billLoginCenterSucess(code):
    return packet.pack("HH",Const.CMD_CT2BILL_LOGIN,code)



#===============================================transpkt======================================================
def transPkt2Client(uid, pkt):
    return packet.pack("HHs",Const.CMD_CT2GATE_TRANS_PKT,uid,pkt)

def transPkt2Game(uid,pkt):
    return packet.pack("HHs",Const.CMD_CT2GAME_TRANS_PKT,uid,pkt)
